package com.csthink.common.infrastructure.enums;

import org.apache.commons.lang3.StringUtils;

/**
 * 当前 web 系统的所有自定义权限
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-13
 */
public enum Authorities {

    CST_USER("VecUser:Read", "VecUser:Edit"),
    CST_INBOX("VecInbox:Read", "VecInbox:Edit");

    private final String readAuthority;

    private final String editAuthority;

    Authorities(String readAuthority, String editAuthority) {
        this.readAuthority = readAuthority;
        this.editAuthority = editAuthority;
    }

    public String readValue() {
        return readAuthority;
    }

    public String editValue() {
        return editAuthority;
    }

    public static boolean isReadPermission(String permission) {
        if (StringUtils.isEmpty(permission)) {
            return false;
        }

        for (Authorities authority : Authorities.values()) {
            String readValue = authority.readValue();
            if (StringUtils.equals(readValue, permission)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isWritePermission(String permission) {
        if (StringUtils.isEmpty(permission)) {
            return false;
        }

        for (Authorities authority : Authorities.values()) {
            String editValue = authority.editValue();
            if (StringUtils.equals(editValue, permission)) {
                return true;
            }
        }
        return false;
    }

    public boolean contain(String permission) {
        if (StringUtils.isEmpty(permission)) {
            return false;
        }

        if (StringUtils.equals(permission, this.readValue())
                || StringUtils.equals(permission, this.editValue())) {
            return true;
        }
        return false;
    }
}
